import * as obj from '../public.js';
import { cookie } from '../../public/cookieFnNew.js';
const o = obj.default;
const petU = cookie.getCookie('petUserD') ? cookie.getCookie('petUserD') : '';
const str = `<h1 class="logo"><img src="../img/logo_w.png" alt=""></h1>
<div class="selection" style="height: 100%; position: relative;">
<div class="people">
  <div class="iconfont icon-gerenfill touxiang"></div>
  <span class="triangle"></span>
</div>
<ul style="display:none">
  <li><span class="iconfont icon-zerenren"></span>个人主页</li>
  <li><span class="iconfont icon-31gouwuche"></span>购物车</li>
  <li><span class="iconfont icon-tuichu"></span>退出登录</li>
</ul>
</div>`;
const st = `<h1 class="logo"><img src="../img/logo_w.png" alt=""></h1>
<div class="top_right" style='display:flex;'>
  <em class="login a">登录</em>
  <em class="register b">注册</em>
</div>`;
console.log(o.$('.top_right'))
if(o.$('.top')){
  const t = o.$('.top')
  if (petU) {
    const top = o.$('.top_right');
    top.remove();
    t.innerHTML = str;
    const selection = o.$('.selection')
    const ul = o.$('.top ul')
    selection.addEventListener('mouseover', function () {
      ul.style.display = 'block'
    })
    selection.addEventListener('mouseleave', function () {
      ul.style.display = 'none'
    })
    ul.addEventListener('click', function (eve) {
      const e = eve || window.event;
      const target = e.target || e.srcElement;
      const li = ul.children;
      const lis = [...li];
      // 通过事件委托给li添加索引
      lis.forEach((val, idx) => {
        val.index = idx
      });
      // 点击之后隐藏下拉菜单
      ul.style.display = 'none';
  
      if (target.index === 0) {
        location.href = './people.html'
      } else if (target.index === 1) {
        location.href = './shopcart.html'
      } else {
        cookie.removeCookie('petUserD')
        selection.remove();
        t.innerHTML = st;
        const a = o.$('.a')
        const b = o.$('.b')
        a.onclick = function () {
          location.href = './login.html'
        }
        b.onclick = function () {
          location.href = './register.html'
        }
      }
    })
  } else {
    const a = o.$('.a')
    const b = o.$('.b')
    // 创建登录事件
    a.onclick = function () {
      location.href = './login.html'
    }
    // 创建注册点击事件
    b.onclick = function () {
      location.href = './register.html'
    }
  }
}
